package examinee

import (
	"admissionsystem/models"
	"admissionsystem/pkg/app"
	"admissionsystem/pkg/e"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetExaminee(c *gin.Context) {
	page := -1
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	limit := -1
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	searchText := ""
	if arg := c.PostForm("searchText"); arg != "" {
		searchText = arg
	}
	examineeParam := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"searchText": searchText,
	}
	err, info, total := models.GetAllExaminee(examineeParam)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

func AddExaminee(c *gin.Context) {
	var examineedata models.Examinee
	c.ShouldBind(&examineedata)
	err, info := models.AddExaminee(examineedata)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "插入成功")
}

func UpadateExaminee(c *gin.Context) {
	var examineedata models.Examinee
	c.ShouldBind(&examineedata)
	err, info := models.UpdateExaminee(examineedata.Eno, examineedata)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "修改成功")
}

func DeleteExaminee(c *gin.Context) {
	var examineedata models.Examinee
	c.ShouldBind(&examineedata)
	err, info := models.DeleteExaminee(examineedata.Eno)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, info, "删除成功")
}